import { Component, OnInit } from '@angular/core';
import { Vibration } from '@ionic-native/vibration/ngx';
import { LocalNotifications } from '@ionic-native/local-notifications/ngx';

@Component({
  selector: 'app-alarmclock',
  templateUrl: './alarmclock.page.html',
  styleUrls: ['./alarmclock.page.scss'],
})
export class AlarmclockPage implements OnInit {
  showClock ;
  nfid = 0;
  datetime ;
 time = 1000 ;
  constructor(private vibration: Vibration,
    private localNotifications: LocalNotifications,
    ) { }

  ngOnInit() {
  }
  startvibration(){
// Duration is ignored on iOS.
this.vibration.vibrate(2000);

// Vibrate 2 seconds
// Pause for 1 second
// Vibrate for 2 seconds
// Patterns work on Android and Windows only
// this.vibration.vibrate([2000,1000,2000]);

// Stop any current vibrations immediately
// Works on Android and Windows only
// this.vibration.vibrate(0);
  }
  stopvibration(){
    // this.vibration.vibrate(0);
  }

  showcloclk(){
     this.time = new Date(this.datetime).getTime()-new Date().getTime();
     console.log('开始');
    this.showClock=setInterval(()=>{
      //实现倒计时的功能代码
      this.shownotification();   
         },this.time );


    
  }
  shownotification() {
    // this.vibration.vibrate(2000);
    this.stopClock();
if(this.showClock === undefined){

  this.showClock=setInterval(()=>{
    //实现倒计时的功能代码
    this.shownotification();   
  },1000*60*1);
  

console.log('shownotification');
    this.localNotifications.schedule({
      id: this.nfid,
      title: '日程提醒',
      text: "日程提醒"+this.nfid,
      icon: 'assets/img/logo.png',
      data: { secret: 'data' },
      autoClear:true,
      vibrate:true,
    });
    this.nfid++;
    this.localNotifications.on('click').subscribe(result => {
     this.stopClock();
    });
  }else {
    this.localNotifications.update({
      id: this.nfid,
      title: '日程提醒',
      text: "日程提醒"+this.nfid,
      icon: 'assets/img/logo.png',
      data: { secret: 'data' },
      autoClear:true,
      vibrate:true,
    });
  }
  }
  stopClock(){
    clearInterval(this.showClock);

}
}
